package com.dj.shop.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dj.shop.util.BeanUtil;
import com.dj.shop.valid.Update;
import com.dj.shop.vo.Result;
import com.dj.shop.vo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Map;


/**
 * @Description:
 *      接口入参校验DEMO
 * @Author: zhaoyu
 * @CreateDate: 2018/9/17 15:58
 * @UpdateUser:
 * @UpdateDate:
 * @UpdateRemark:
 */
@RestController
@Validated
@RequestMapping("/check")
public class CheckDemoController extends BaseController {

    /**
     * 日志
     */
    private final Logger logger = LoggerFactory.getLogger(CheckDemoController.class);

    /**
     *
     * @description
     *      自定义对象入参校验
     * @date: 2018/9/17 15:59
     * @author: zhaoyu
     * @UpdateUser:
     * @UpdateRemark:
     * @param user
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/object")
    public Result checkObject(@Validated({Update.class}) User user, BindingResult bindingResult){

        // 入参校验
        String validResult = this.getValidResult(bindingResult);
        if ( validResult!= null){
            return Result.error(validResult);
        }

        // 对象转换Map
        Map parMap = null;
        try {
            parMap = BeanUtil.convertToMap(user);
            logger.info("parMap:"+parMap);
        } catch (Exception e) {
            logger.error("对象转换异常：",e);
            return Result.error("对象转换异常");
        }

        //逻辑处理
        Result result = null;
        try {
            result = Result.success();
        }catch (Exception e) {
            result = Result.error();
            logger.error("操作失败：{}", e);
        }

        return result;
    }



    /**
     *
     * @description
     *      非对象入参校验
     * @date: 2018/9/17 15:59
     * @author: zhaoyu
     * @UpdateUser:
     * @UpdateRemark:
     * @param name
     * @return
     */
    @PostMapping(value = "/one")
    public Result checkOne(@NotNull(message = "name 不能为空")  String name){

        Result result = null;
        try {
            result = Result.success("");
        }catch (Exception e) {
            result = Result.error();
            logger.error("操作失败：{}", e);
        }

        return result;
    }

    /**
     * json例子
     * @param json
     * @return
     */
    @PostMapping(value = "/json")
    public Result json(@RequestBody String json){
        Map map = JSON.parseObject(json);
        String obj = map.get("cartGoods")+"";
        JSONArray jsonArray = JSON.parseArray(obj);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String shopGoodId = String.valueOf(jsonObject.get("shopGoodId"));
            String shopName = String.valueOf(jsonObject.get("shopName"));
            logger.info("shopGoodId---->"+shopGoodId);
            logger.info("shopName---->"+shopName);
        }
        return Result.success();
    }

}